package tcloudclient.treeTable;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import tcloudclient.contex.Context;
import tcloudclient.fileSystem.FileNode;
import tcloudclient.fileSystem.FileStatusEnum;

/**
 * FileSystemModel is a TreeTableModel representing a hierarchical file system.
 * Nodes in the FileSystemModel are FileNodes which, when they are directory
 * nodes, cache their children to avoid repeatedly querying the real file
 * system.
 *
 * @version %I% %G%
 *
 * @author Philip Milne
 * @author Scott Violet
 */
public class FileSystemModel extends AbstractTreeTableModel{

    // Names of the columns.
    static protected String[] cNames = {"Jméno", "Typ", "Stav", "Velikost", "Poslední změna"};
    // Types of the columns.
    static protected Class[] cTypes = {TreeTableModel.class, String.class, FileStatusEnum.class, Integer.class, String.class};
    // The the returned file length for directories. 
    public static final Integer ZERO = new Integer(0);
    
    private DateFormat dateFormat = new SimpleDateFormat("HH:mm dd.MM.yyyy");

    public FileSystemModel(FileNode fileNode) {
        super(fileNode);
    }

    //
    // Some convenience methods. 
    //
    protected FileNode getFile(Object node) {
        FileNode fileNode = ((FileNode) node);
        return fileNode;
    }

    protected Object[] getChildren(Object node) {
        FileNode fileNode = ((FileNode) node);
        return fileNode.getChilds().toArray();
    }

    //
    // The TreeModel interface
    //
    @Override
    public int getChildCount(Object node) {
        Object[] children = getChildren(node);
        return (children == null) ? 0 : children.length;
    }

    @Override
    public Object getChild(Object node, int i) {
        return getChildren(node)[i];
    }

    // The superclass's implementation would work, but this is more efficient. 
    @Override
    public boolean isLeaf(Object node) {
        return getFile(node).isLeaf();
    }

    //
    //  The TreeTableNode interface. 
    //
    @Override
    public int getColumnCount() {
        return cNames.length;
    }

    @Override
    public String getColumnName(int column) {
        return cNames[column];
    }

    @Override
    public Class getColumnClass(int column) {
        return cTypes[column];
    }

    @Override
    public Object getValueAt(Object node, int column) {
        FileNode file = getFile(node);
        try {
            switch (column) {
                case 0:
                    return file.getFile().getName();
                case 2:
                    return file.getStatus();
                case 3:
                    return file.getFile().isFile() ? new Integer((int) file.getFile().length()) : ZERO;
                case 1:
                    return file.isLeaf() ? "Soubor" : "Složka";
                case 4:
                    return dateFormat.format(new Date(file.getLastChange()));
            }
        } catch (SecurityException se) {
        }

        return null;
    }
}
